package com.njwb.day07.Stus;

import java.util.Arrays;

public class StudentDaoImpl implements StudentDao {
    private static Student[] stus = new Student[0];


    @Override
    public void add() {
        Student s1 = new Student();
        s1.setNo(10001);
        s1.setName("张三");
        s1.setScores(88.5);


        Student s2 = new Student();
        s2.setNo(10002);
        s2.setName("李四");
        s2.setScores(92.0);

        Student s3 = new Student();
        s3.setNo(10003);
        s3.setName("王五");
        s3.setScores(79.5);

        Student s4 = new Student();
        s4.setNo(10004);
        s4.setName("赵六");
        s4.setScores(85.0);

        stus = new Student[]{s1, s2, s3, s4};
    }

    @Override
    public Student SelectNo(int no) {
        for (int i = 0; i < stus.length; i++) {
            if (stus[i].getNo() == no) {
                return stus[i];
            }
        }
        return null;
    }

    @Override
    public String addStudent(int no, String name, double scores) {
        stus = Arrays.copyOf(stus, stus.length + 1);
        stus[stus.length - 1] = new Student();
        stus[stus.length - 1].setNo(no);
        stus[stus.length - 1].setName(name);
        stus[stus.length - 1].setScores(scores);
        return "添加成功";
    }

    @Override
    public Student[] findAll() {
        return stus; //
    }

    @Override
    public void alterStudent(int no, String name, double score) {

        for (int i = 0; i < stus.length; i++) {
            if (stus[i].getNo() == no) {
                stus[i].setName(name);
                stus[i].setScores(score);

                return;
            }
        }
    }

    @Override
    public void deleteStudent(int no) {
        for (int i = 0; i < stus.length; i++) {
            if (stus[i].getNo() == no) {
                Student[] newstus = new Student[stus.length - 1];
                System.arraycopy(stus, 0, newstus, 0, i);

                System.arraycopy(stus, i + 1, newstus, i, stus.length - i - 1);


                stus = newstus;


                return;
            }
        }
    }

    @Override
    public Student showName(String name) {
        for (int i = 0; i < stus.length; i++) {
            if (stus[i].getName().equals(name)) {
                return stus[i];

            }
        }
        return null;
    }

    @Override
    public Student[] showDesc() {
        Student[] newstus = Arrays.copyOf(stus, stus.length);
        for (int i = 0; i < stus.length - 1; i++) {
            for (int k = i; k < stus.length; k++) {
                if (newstus[i].getScores() < newstus[k].getScores()) {
                    Student a = newstus[i];
                    newstus[i] = newstus[k];
                    newstus[k] = a;
                }
            }
        }
        return newstus;
    }


}
